package caches

// CacheMag 缓存管理器
// 已废弃，请使用 CacheFacade
type CacheMag struct {
	m map[string]ItfCache
}

func NewCacheMag() *CacheMag {
	return &CacheMag{}
}

func (mag *CacheMag) AddCache(c ItfCache, names ...string) {
	if mag.m == nil {
		mag.m = map[string]ItfCache{}
	}
	mag.m[CombineKeys(names...)] = c
}

func (mag *CacheMag) Get(keys ...string) (string, bool, error) {
	if len(keys) <= 0 {
		return "", false, nil
	}
	key := keys[len(keys)-1] //最后一个为key
	ca := mag.Cache(keys[:len(keys)-1]...)
	if ca == nil {
		return "", false, nil
	}
	return ca.Get(key)
}

func (mag *CacheMag) Clear(keys ...string) {
	if len(keys) <= 0 {
		return
	}
	key := keys[len(keys)-1] //最后一个为key
	ca := mag.Cache(keys[:len(keys)-1]...)
	if ca == nil {
		return
	}
	ca.Delete(key)
	return
}

func (mag *CacheMag) Cache(keys ...string) ItfCache {
	if mag.m == nil {
		return nil
	}
	ca, exist := mag.m[CombineKeys(keys...)]
	if !exist {
		return nil
	}
	return ca
}
